<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件委托</title>
</head>

<body>
    <ul id="parent-list">
        <li id="post-1">Item 1</li>
        <li id="post-2">Item 2</li>
        <li id="post-3">Item 3</li>
        <li id="post-4">Item 4</li>
        <li id="post-5">Item 5</li>
        <li id="post-6">Item 6</li>
        <a class="classA classB" href="www.baidu.com">class A</a>
    </ul>
    <script>
        // Get the element, add a click listener...
        document.getElementById("parent-list").addEventListener("click", function (e) {
            // e.target is the clicked element!
            // If it was a list item
            if (e.target && e.target.nodeName == "LI") {
                // List item found!  Output the ID!
                console.log("List item ", e.target.id.replace("post-", ""), " was clicked!");
            }
        });
        document.getElementById("parent-list").addEventListener("click",function(e) {
            // e.target was the clicked element
          e.preventDefault();
          console.log(e);
          if (e.target && e.target.matches("a.classA")) {
            console.log("Anchor element clicked!");
            }
        });
    </script>
</body>

</html>